package com.day12.study.condtion_demo;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class ProductBase {
    private String name;
    private int price;
    private boolean isFull = false;
    private final ReentrantLock lock = new ReentrantLock();
    private final Condition condition = lock.newCondition();

    public synchronized void put(String paraName, int paraPrice) {
        try {
            lock.lock();
            if (isFull) {
                try {
                    condition.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            this.name = paraName;
            this.price = paraPrice;
            isFull = true;
            condition.signal();
        } finally {
            lock.unlock();
        }

    }

    public synchronized void get() {
        try {
            lock.lock();
            if (!isFull) {
                try {
                    condition.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println(name + ":" + price);
            isFull = false;
            condition.signal();
        } finally {
            lock.unlock();
        }
    }
}
